Skip to content

Broaden receive data buffer types#197

Open
Mirochill wants to merge 1 commit into
python-hyper:masterfrom
Mirochill:fix-186-buffer-types
Open

Broaden receive data buffer types#197
Mirochill wants to merge 1 commit into
python-hyper:masterfrom
Mirochill:fix-186-buffer-types

Conversation

@Mirochill
Copy link
Copy Markdown

Summary

  • widen Connection.receive_data() and the internal receive buffer annotations to accept bytearray and memoryview alongside bytes
  • add a regression test covering both supported bytes-like inputs
  • document the backward-compatible API expansion in the news fragment

Why

Issue #186 points out that receive_data() already operates on bytes-like input, but its public annotation is still narrower than the runtime behavior. Widening the concrete accepted types removes false positives for downstream users adopting stricter bytes checking while keeping this patch intentionally small for h11's current typing/tooling baseline.

Validation

  • No local validation run, per this workspace's coordination-only policy.
  • Performed static review of the receive path and confirmed Connection.receive_data() forwards directly into ReceiveBuffer.__iadd__().
  • Verified the patch shape with git diff --check.
  • Relying on the repository's remote CI for execution feedback.

Fixes #186.

@Mirochill Mirochill marked this pull request as ready for review May 18, 2026 20:57
Comment thread h11/_connection.py
return (bytes(self._receive_buffer), self._receive_buffer_closed)

def receive_data(self, data: bytes) -> None:
def receive_data(self, data: Union[bytes, bytearray, memoryview]) -> None:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use collections.abc.Buffer for Python >= 3.12, as introduced in PEP 688. Alternatively, you can use if typing.TYPE_CHECKING: from typing_extensions import Buffer, which avoids a runtime dependency on typing_extensions.

Comment thread h11/_receivebuffer.py

def __iadd__(self, byteslike: Union[bytes, bytearray]) -> "ReceiveBuffer":
def __iadd__(
self, byteslike: Union[bytes, bytearray, memoryview]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto



@pytest.mark.parametrize("data_wrapper", [bytearray, memoryview])
def test_receive_data_accepts_byteslike_objects(data_wrapper: Any) -> None:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this parameter need to be Any?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update types to adhere to --strict-bytes / PEP688

2 participants